package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.SmartBeep;
import com.moleq.device.DeviceManager;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.enums.ItemType;
import com.moleq.logic.SaleList;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.R;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.models.SaleListItem;
import com.moleq.service.ServiceManager;
import com.moleq.util.BigDecmialTool;

public class ItemDiscountDialog2 extends MoleQBaseDialog {

	private Button mButtonBack, mButtonOk, mButtonRemove;
	private TransactionManager mPaymentMgr;
	private ListView mListView;
	private List<ItemDiscountSetting> mDataSource = new ArrayList<ItemDiscountSetting>();
	private OpenDiscountAdapter mAdapter;
	private DeviceManager mDeviceManager; // contral device scanner or scale
	private int selectedRow = 0;
	private SaleList saleList;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnItemDntBack:
				mDeviceManager.enableAllScanner();
				dismiss();
				break;
			case R.id.btnItemDntRemove:

				MessageBox.show(mContext, "Do you want to remove the item discount?", mOnClickMessageListener, MessageDialog.MESSAGE_BOX_YES_NO);
				break;
			case R.id.btnItemDntOk:

				if (mDataSource.size() > 0) {
					ItemDiscountSetting discount = mDataSource.get(selectedRow);

					int itemIdx = saleList.getSelectedRegularItemIdx();
					if (itemIdx >= 0) {
						SaleListItem item = saleList.getItem(itemIdx);
//						SaleListItem discntAmtItem = saleList.createItem();
						
//						SaleListItem discntAmtItem = saleList.createItemDiscount(item,-1,discount,0);
//
////						discntAmtItem.setItemType(ItemType.ITEM_DISCOUNT_MANUAL);
////						discntAmtItem.setUpc("ITEM_DISCOUNT_MANUAL");
////						discntAmtItem.setParentId(item.getId());
////						discntAmtItem.setPoslineno(item.getPoslineno());
////						discntAmtItem.setTax1(item.getTax1());
////						discntAmtItem.setTax2(item.getTax2());
////						discntAmtItem.setTax3(item.getTax3());
////						discntAmtItem.setTax4(item.getTax4());
////						discntAmtItem.setTax5(item.getTax5());
////						discntAmtItem.setTax6(item.getTax6());
////						discntAmtItem.setFoodstamp(item.getFoodstamp());
////						discntAmtItem.setNodiscount(item.getNodiscount());
////						discntAmtItem.setPoslinediscntno(discount.getDs_no());
////
////						String desc = "";
////						desc =  discount.getDs_desc();
////						discntAmtItem.setName(desc);
////						discntAmtItem.setDisplayContent(desc);
////
////						double dnt = discount.getDs_value();
////						dnt = BigDecmialTool.div(dnt, 100);
////						dnt = BigDecmialTool.mul(item.getPrice(), dnt);
////						dnt = BigDecmialTool.mul(-1, dnt);
////
////						discntAmtItem.setPrice(dnt);
////						discntAmtItem.setQty(1);
//
//						saleList.addItemToList(discntAmtItem);
//						sa.mRefundMode = false; //cancel refund
//						GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
//						mPaymentMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_ITEM_DISOCUNT, 1, discntAmtItem.getPrice());
					}

					mDeviceManager.enableAllScanner();
					dismiss();
				}
				break;
			}
		}
	};

	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_discount_list:
				ScrollToRow(position);
				break;
			}
		}
	};

	public ItemDiscountDialog2(Context context) {
		super(context, R.style.dialog, context);
		mContext = context;
		
	}


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.dialog_item_discount_selection);
		this.setCancelable(false);
		mDeviceManager = DeviceManager.getInstance();
		mPaymentMgr = TransactionManager.getInstance();

		mButtonBack = (Button) findViewById(R.id.btnItemDntBack);
		mButtonBack.setOnClickListener(mOnClickListener);

		mButtonOk = (Button) findViewById(R.id.btnItemDntOk);
		mButtonOk.setOnClickListener(mOnClickListener);

		mButtonRemove = (Button) findViewById(R.id.btnItemDntRemove);
		mButtonRemove.setOnClickListener(mOnClickListener);

		mListView = (ListView) findViewById(R.id.lv_dialog_discount_list);
		mListView.setOnItemClickListener(onItemClickListener);

		mAdapter = new OpenDiscountAdapter(mContext);
		mListView.setAdapter(mAdapter);
		mListView.setDivider(null);

	}

	public void show() {
		super.show();

		mDeviceManager.disableAllScanner();
		mDataSource = ServiceManager.getInstance().mDiscountSettingService.FindAll();
		mAdapter.notifyDataSetChanged();

		saleList = TransactionManager.getInstance().mSaleListItems;
		// itemIdx = saleList.getLastItemIdx();
	}

	class OpenDiscountAdapter extends BaseAdapter {
		private Context maContext;

		public final class ViewHolder {
			public TextView tv_discount_name;
			public TextView tv_discount_value;
		}

		public OpenDiscountAdapter(Context context) {
			maContext = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataSource.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataSource.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = LayoutInflater.from(maContext).inflate(R.layout.discount_list_item, null);
				holder.tv_discount_name = (TextView) convertView.findViewById(R.id.tv_discount_name);
				holder.tv_discount_value = (TextView) convertView.findViewById(R.id.tv_discount_value);

				convertView.setOnTouchListener(onItemTouchListener);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			String content = mDataSource.get(position).getDs_desc();
			String value = String.valueOf(mDataSource.get(position).getDs_value());
			String type = mDataSource.get(position).getDs_type();
			
			holder.tv_discount_name.setText(content);
			if (type.equals("P")) {
				type = "%";
				holder.tv_discount_value.setText(value+type);
			}else {
				double amount = Double.valueOf(value);
				amount = BigDecmialTool.mul(-1, amount);
				holder.tv_discount_value.setText(String.format("%.2f", amount)+"   ");
			}

			if (position == selectedRow) {
				holder.tv_discount_name.setBackgroundColor(Color.rgb(255, 219, 16));
				holder.tv_discount_value.setBackgroundColor(Color.rgb(255, 219, 16));
			} else {
				holder.tv_discount_name.setBackgroundColor(Color.rgb(204, 204, 204));
				holder.tv_discount_value.setBackgroundColor(Color.rgb(204, 204, 204));
			}
			//
			return convertView;
		}

	}

	private OnTouchListener onItemTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				SmartBeep.getInstance().play(SmartBeep.SOUND_CLICK);
			}
			return false;
		}

	};

	public void ScrollToRow(int row) {
		selectedRow = row;
		mAdapter.notifyDataSetChanged();
	}

	private OnClickMessageListener mOnClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			int itemIdx = saleList.getSelectedRegularItemIdx();
			if (itemIdx >= 0) {
				saleList.removeChildItem(itemIdx , ItemType.ITEM_DISCOUNT_PERECENT);
			}

			mDeviceManager.enableAllScanner();
			dismiss();
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickOK() {

		}

	};

	

}
